﻿using System.ServiceModel;
using System.Web.Security;
using VASJ.Model;

namespace VASJ.Provider
{
    public class AcpInetRoleProvider : RoleProvider
    {
        BasicHttpBinding _binding;
        EndpointAddress _endpoint;

        string _ApplicationName = string.Empty;

        string username = string.Empty;
        string password = string.Empty;

        /// <summary>
        /// Init
        /// </summary>
        public AcpInetRoleProvider()
        {
            BaseHelper.GetConfig(out _binding, out _endpoint, out _ApplicationName, out username, out password);
        }

        /// <summary>
        /// ApplicationName
        /// </summary>
        public override string ApplicationName
        {
            get
            {
                return this._ApplicationName;
            }
            set
            {
                this._ApplicationName = value;
            }
        }

        /// <summary>
        /// AddUsersToRoles
        /// </summary>
        /// <param name="usernames"></param>
        /// <param name="roleNames"></param>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            acpiNet.AddUsersToRoles(usernames, roleNames);
        }

        /// <summary>
        /// CreateRole
        /// </summary>
        /// <param name="roleName"></param>
        public override void CreateRole(string roleName)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            acpiNet.CreateRole(roleName);
        }

        /// <summary>
        /// DeleteRole
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="throwOnPopulatedRole"></param>
        /// <returns></returns>
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.DeleteRole(roleName, throwOnPopulatedRole);
        }

        /// <summary>
        /// FindUsersInRole
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="usernameToMatch"></param>
        /// <returns></returns>
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.FindUsersInRole(roleName, usernameToMatch);
        }

        /// <summary>
        /// GetAllRoles
        /// </summary>
        /// <returns></returns>
        public override string[] GetAllRoles()
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetAllRoles(this._ApplicationName);
        }

        /// <summary>
        /// GetRolesForUser
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public override string[] GetRolesForUser(string username)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetRolesForUser(this._ApplicationName, username);
        }

        /// <summary>
        /// GetRolesForUserInProject
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public string[] GetRolesForUserInProject(string ApplicationName, int projectId, string username)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetRolesForUserInProject(this._ApplicationName, projectId, username);
        }

        /// <summary>
        /// GetUsersInRole
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override string[] GetUsersInRole(string roleName)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetUsersInRole(this._ApplicationName, roleName);
        }

        /// <summary>
        /// GetUsersInRole
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public string[] GetUsersInRole(int projectId, string roleName, bool include_via_org = true)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetUsersInRoleForProject(this._ApplicationName, projectId, roleName, include_via_org);
        }

        /// <summary>
        /// IsUserInRole
        /// </summary>
        /// <param name="username"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override bool IsUserInRole(string username, string roleName)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.IsUserInRole(this._ApplicationName, username, roleName);
        }

        /// <summary>
        /// IsRoleOfUserInProject
        /// </summary>
        /// <param name="username"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public bool IsRoleOfUserInProject(int projectId, string username, string roleName)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.IsRoleOfUserInProject(this._ApplicationName, projectId, username, roleName);
        }

        /// <summary>
        /// RemoveUsersFromRoles
        /// </summary>
        /// <param name="usernames"></param>
        /// <param name="roleNames"></param>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            acpiNet.RemoveUsersFromRoles(this._ApplicationName, usernames, roleNames);
        }

        /// <summary>
        /// RoleExists
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override bool RoleExists(string roleName)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.RoleExists(roleName);
        }
        public Role[] GetAllRole()
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetAllRole(this._ApplicationName);

        }
        #region "AcpInetRoleType"
        public Role[] GetRoleList(string username, bool ispm = false, int prjtype = 0)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetRoleList(this._ApplicationName, username, ispm, prjtype);

        }
        #endregion

    }
}